/*
   (C) 2007 - Michael Ocean - Boston University

  This work is a part of the larger snBench project.  Information
  can be found at: http://csr.bu.edu/snbench

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package stepide.STEPVisualizer;

import java.io.Serializable;

import org.jgraph.graph.CellView;
import org.jgraph.graph.CellViewFactory;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.GraphModel;
import org.jgraph.graph.PortView;
import org.jgraph.graph.VertexView;

import step.AbstractTrigger;
import step.ExpNode;
import step.LastTriggerEval;
import step.ReceiverSocketNode;
import step.SenderSocketNode;
import step.SensorNode;
import step.SpliceNode;
import step.ValueNode;

import stepide.STEPVisualizer.JStepPanel.FakeStepNode;

public class MyCellViewFactory implements CellViewFactory, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public CellView createView(GraphModel model, Object cell) {
		CellView view = null;
		if (model.isPort(cell))
			view = createPortView(cell);
		else if (model.isEdge(cell))
			view = createEdgeView(cell);
		else
			view = createVertexView(cell);
		return view;
	}

	/**
	 * Constructs a VertexView view for the specified object.
	 */
	protected VertexView createVertexView(Object cell) {
		if(cell instanceof DefaultGraphCell)
			if(((DefaultGraphCell)cell).getUserObject() instanceof FakeStepNode){
				
				FakeStepNode fsn = (FakeStepNode)((DefaultGraphCell)cell).getUserObject();
				if((fsn.getNode() instanceof ValueNode) 
						|| (fsn.getNode() instanceof SensorNode)
						|| (fsn.getNode() instanceof ExpNode)) 
					return new JGraphRoundRectView(cell);
				if(fsn.getNode() instanceof AbstractTrigger)
					return new JGraphOctoView(cell);
				if((fsn.getNode() instanceof LastTriggerEval) 
						|| (fsn.getNode() instanceof SpliceNode))
					return new JGraphElipseView(cell);
				if(fsn.getNode() instanceof SenderSocketNode)
					return new JGraphDownwardView(cell);
				if(fsn.getNode() instanceof ReceiverSocketNode)
					return new JGraphUpwardView(cell);				
				return new VertexView(cell);

			}
		return new VertexView(cell);
	}

	/**
	 * Constructs an EdgeView view for the specified object.
	 */
	protected EdgeView createEdgeView(Object cell) {
			return new EdgeView(cell);
	}

	/**
	 * Constructs a PortView view for the specified object.
	 */
	protected PortView createPortView(Object cell) {
			return new PortView(cell);
	}
}

